package base_grammar

import "fmt"

type Animal struct {
	name string
}

func (animal Animal) eat() {
	fmt.Printf("animal.name: %v  eat... \n", animal.name)
}

func (animal Animal) sleep() {
	fmt.Printf("animal.name: %v  slepp... \n", animal.name)
}

type Dog3 struct {
	Animal
	age int
}

type Cat3 struct {
	Animal
}

func interface_f3() {
	// 模拟继承
	c := Cat3{Animal{name: "cat"}}
	d := Dog3{Animal{name: "dog"}, 20}

	c.eat()
	c.sleep()
	d.eat()
	d.sleep()
}

func interface_f4() {
	// 模拟构造函数
	p, err := NewPerson("tom", 20)
	if err == nil {
		fmt.Printf("p: %v\n", *p)
	} else {
		fmt.Printf("err: %v\n", err)
	}
}

func NewPerson(name string, age int) (*Person3, error) {
	if name == "" {
		return nil, fmt.Errorf("name不能为空")
	}
	if age < 0 || age > 120 {
		return nil, fmt.Errorf("age不能小于0或者大于120")
	}
	return &Person3{name: name, age: age}, nil
}

type Person3 struct {
	name string
	age  int
}
